package com.zcs.app.lucky.ui

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import androidx.lifecycle.lifecycleScope
import cn.sxw.android.base.kt.BaseNormalKtActivity
import cn.sxw.android.base.kt.BaseViewModel
import cn.sxw.android.base.net.CustomNetConfig
import cn.sxw.android.base.okhttp.HttpManager
import cn.sxw.android.base.utils.JLogUtil
import cn.sxw.android.base.utils.MMKVUtils
import com.zcs.app.lucky.BuildConfig
import com.zcs.app.lucky.databinding.ActivitySplashBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class SplashActivity : BaseNormalKtActivity<ActivitySplashBinding, BaseViewModel>() {
    override fun needViewModel(): Boolean = false

    override fun onInit() {
        initView()
        // 初始化服务器信息
        val cacheVal: Int? = MMKVUtils.getInteger(CustomNetConfig.KEY_CURR_ENVIRONMENT, -1)
        if (cacheVal != null && cacheVal > -1) {
            CustomNetConfig.updateEnvironment(cacheVal.toInt())
        } else {
            CustomNetConfig.updateEnvironment(BuildConfig.ENVI_HOST_INDEX)
        }
        JLogUtil.d("当前服务器类型为 >>> " + CustomNetConfig.getCurrServerTypeName())
        JLogUtil.d("当前服务器类型为 >>> " + HttpManager.getInstance().host)
        JLogUtil.d("DeviceUtils.isTablet = ${isTablet()}")
    }

    override fun addOnClickListener() {
        binding.btnEnter.setOnClickListener {
            intoApp()
        }
    }

    override fun initObserve() {
    }

    override fun initView() {
        lifecycleScope.launch {
            withContext(Dispatchers.Main) {
                // 动画Name
                delay(300)
                if (isFinishing)
                    return@withContext
                animName()

                // 显示按钮
                delay(100)
                if (isFinishing)
                    return@withContext
                showEnterBtn()
            }
        }
    }

    private suspend fun animName() {
        val r0 = binding.tvName.rotation
        val ar = ObjectAnimator.ofFloat(binding.tvName, "rotation", r0, r0 + 360 * 1f)
        ar.duration = 400
        ar.start()
        delay(600)
        if (isFinishing)
            return
        binding.tvName.visibility = View.VISIBLE
        val asx = ObjectAnimator.ofFloat(binding.tvName, "scaleX", 1f, 2f, 1.5f)
        val asy = ObjectAnimator.ofFloat(binding.tvName, "scaleY", 1f, 2f, 1.5f)

        val rp = binding.ivPan.rotation
        val arp = ObjectAnimator.ofFloat(binding.ivPan, "rotation", rp, rp + 360)
        val set = AnimatorSet()
        set.playTogether(asx, asy, arp)
        set.duration = 666
        set.start()
    }

    private suspend fun showEnterBtn() {
        binding.btnEnter.visibility = View.VISIBLE
        val anim1 = ObjectAnimator.ofFloat(binding.btnEnter, "alpha", 0f, 1f)
        val anim2 = ObjectAnimator.ofFloat(binding.btnEnter, "scaleX", 0f, 1.5f, 1f)
        val anim3 = ObjectAnimator.ofFloat(binding.btnEnter, "scaleY", 0f, 1.5f, 1f)
        val set = AnimatorSet()
        set.playTogether(anim1, anim2, anim3)
        set.duration = 666
        set.start()
        delay(1666)
        for (c in intArrayOf(3, 2, 1)) {
            binding.btnEnter.text = "进入($c)"
            delay(998)
        }
        intoApp()
    }

    private fun intoApp() {
        // 进入主页
        JLogUtil.d("进入主页")
        launchActivity(LuckyHomeActivity::class.java, killIt = true)
    }
}